

void create(int32_t memSize)
{
	RAM = (char*) malloc(memSize);
}

void runCode(uint32_t address)
{
	pos = address;
	while(RAM[pos] != 0)
	{
		int32_t instruction_address = 0;
		unsigned char opcode = RAM[pos];
		pos++;
		char arglength = RAM[pos];
		pos++;
		if(opcode == BEQ)
		{
			int32_t  np = (int32_t)readInt32();
			printf("beq %s\n",getSymName(theProgram, np + (pos )));
		}
		else if(opcode == BNE)
		{
			int32_t  np = (int32_t)readInt32();
			printf("bne %s\n",getSymName(theProgram, np + (pos )));
		}
		else if(opcode == BGT)
		{
			int32_t  np = (int32_t)readInt32();
			printf("bgt %s\n",getSymName(theProgram, np + (pos )));
		}
		else if(opcode == BLT)
		{
			int32_t  np = (int32_t)readInt32();
			printf("blt %s\n",getSymName(theProgram, np + (pos )));
		}
		else if(opcode == BZ)
		{
			int32_t  np = (int32_t)readInt32();
			printf("bz %s\n",getSymName(theProgram, np + (pos )));
		}
		else if(opcode == TEQ)
		{
			printf("teq\n");
		}
		else if(opcode == TNE)
		{
			printf("tne\n");
		}
		else if(opcode == TGT)
		{
			printf("tgt\n");
		}
		else if(opcode == TLT)
		{
			printf("tlt\n");
		}
		else if(opcode == 0xF0)
		{
			int32_t  np = (int32_t)readInt32();
			printf("blt %s\n",getSymName(theProgram, np + (pos )));
		}
		else if(opcode == BRA)
		{
			int32_t  np = (int32_t)readInt32();
			printf("bra %s\n",getSymName(theProgram, np + (pos)));
		}
		
		else if(opcode == BSR)
		{
			int32_t  np = (int32_t)readInt32();
			printf("bsr %s\n",getSymName(theProgram, np + (pos)));
		}
		else if (opcode == BEGIN_FAULT)
		{
			int32_t  np = (int32_t)readInt32();
			printf("begin_fault %s\n",getSymName(theProgram, np + (pos )));
		}
		else if (opcode == END_FAULT)
		{
			printf("end_fault\n");
		}
		else if (opcode == THROW)
		{
			continue;
		}
		else if (opcode == ADD)
		{
			printf("add\n");
		}
		else if (opcode == FADD)
		{
			printf("fadd\n");
		}
		else if (opcode == SUB)
		{
			printf("sub\n");
		}
		else if (opcode == FSUB)
		{
			printf("fsub\n");
		}
		else if (opcode == MUL)
		{
			printf("mul\n");
		}
		else if (opcode == FMUL)
		{
			printf("fmul\n");
		}
		else if (opcode == MUL_S)
		{
			printf("fmul_s\n");
		}
		else if (opcode == DIV)
		{
			printf("div\n");
		}
		else if (opcode == DIV_S)
		{
			printf("div_s\n");
		}
		else if (opcode == FDIV)
		{
			printf("fdiv\n");
		}
		else if (opcode == BOR)
		{
			printf("bor\n");
		}
		else if (opcode == XOR)
		{
			printf("xor\n");
		}
		else if (opcode == AND)
		{
			printf("and\n");
		}
		else if (opcode == STELEM_D)
		{
			printf("stelem_d\n");
		}
		else if (opcode == LDELEM_Q)
		{
			printf("ldelem_q\n");
		}
		else if (opcode == LDELEM_D)
		{
			printf("ldelem_d\n");
		}
		else if (opcode == LDELEM_W)
		{
			printf("ldelem_w\n");
		}
		else if (opcode == LDELEM_B)
		{
			printf("ldelem_b\n");
		}
		else if (opcode == DUP)
		{
			if(arglength == 8)
			{
				readInt64();
				printf("dup_q\n");
			}
			else if(arglength == 4)
			{
				readInt32();
				printf("dup_d\n");
			}
			else if(arglength == 2)
			{
				readInt16();
				printf("dup_w\n");
			}
			else if(arglength == 1)
			{
				pos++;
				printf("dup_b\n");
			}
		}
		else if (opcode == PUSH)
		{
			
			if(arglength == 8)
			{
				printf("push qword %d\n",readInt64());
			}
			else if(arglength == 4)
			{
				uint32_t data = readInt32();
				printf("push dword %d\n",data);
			}
			else if (arglength == 2)
			{
				printf("push word %d\n",readInt16());
			}
			else if (arglength == 1)
			{
				printf("push byte %d\n",readInt8());
			}
		}
		else if (opcode == SYSF)
		{
			int32_t function = readInt32();
			printf("sysf %d\n",function);
		}
		else if (opcode == POP)
		{
			if(arglength == 4)
			{
				printf("pop_d\n");
				readInt32();
			}
			else if (arglength == 2)
			{
				printf("pop_w\n");
				readInt16();
			}
		}
		else if (opcode == RET)
		{
			printf("ret\n");
		}
		else if (opcode == LOAD)
		{
			if(arglength == 8)
			{
				readInt64();
				printf("qload\n");
			}
			else if(arglength == 4)
			{
				readInt32();
				printf("dload\n");
			}
			else if (arglength == 2)
			{
				readInt16();
				printf("wload\n");
				
			}
			else if (arglength == 1)
			{
				pos++;
				printf("bload\n");
			}
		}
		else if (opcode == STORE)
		{
			if(arglength == 8)
			{
				readInt64();
				printf("qstore\n");
			}
			else if(arglength == 4)
			{
				readInt32();
				printf("dstore\n");
			}
			else if (arglength == 2)
			{
				readInt16();
				printf("wstore\n");
				
			}
			else if (arglength == 1)
			{
				pos++;
				printf("bstore\n");
			}
		}
		if(getSymName(theProgram,pos) != 0)
		{
			printf("%s:\n",getSymName(theProgram, pos));
		}
	}

}
